//
//  QWBaseController.m
//  qwm
//
//  Created by kevin on 2023/3/23.
//

#import "QWBaseController.h"

@interface QWBaseController ()
@property (nonatomic,strong) UIView *lineView;
@end

@implementation QWBaseController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//     [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    
    UIImageView *navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
        // 隐藏导航栏下的黑线
        navBarHairlineImageView.hidden = YES;
    
    [self customNavigationBar];
    
    [self leftBarButtonItem];//返回

}

#pragma mark- 自定义导航栏
-(void)customNavigationBar
{
    //导航栏背景色
    UIColor *navBarColor = Color_Main_Green;
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        appearance.backgroundColor = navBarColor;
        appearance.backgroundEffect = nil;
        appearance.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
        self.navigationController.navigationBar.standardAppearance = appearance;
    }else{
        [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:navBarColor] forBarMetrics:UIBarMetricsDefault];
        //设置导航栏文字颜色
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    }
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

}

#pragma mark- 导航栏左
-(UIBarButtonItem *)leftBarButtonItem
{
    if (!_leftBarButtonItem) {
        _leftBarButtonItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"WnavBack.png"] style:UIBarButtonItemStylePlain target:self action:@selector(navLeftBaseButtonClick)];
        _leftBarButtonItem.imageInsets=UIEdgeInsetsMake(0, -10, 0, 0);
        self.navigationItem.leftBarButtonItem =_leftBarButtonItem;
    }
    return _leftBarButtonItem;
}
#pragma mark - 导航栏左 - 隐藏\显示
- (void)setIsHiddenLeftBarButtonItem:(BOOL)isHiddenLeftBarButtonItem
{
    _isHiddenLeftBarButtonItem = isHiddenLeftBarButtonItem;
    
    if (@available(iOS 16.0, *)) {
        [_leftBarButtonItem setHidden:_isHiddenLeftBarButtonItem];
    } else {
//            self.navigationItem.leftBarButtonItem = nil;
        _leftBarButtonItem.image = _isHiddenLeftBarButtonItem?[UIImage new]:[UIImage imageNamed:@"WnavBack.png"];

    }
}
#pragma mark-导航栏右
-(UIBarButtonItem *)rightBarButtonItem
{
    if (!_rightBarButtonItem) {
        _rightBarButtonItem= [[UIBarButtonItem alloc]init];
        _rightBarButtonItem.style=UIBarButtonItemStylePlain;
        [_rightBarButtonItem setTintColor:[UIColor whiteColor]];
//        [_rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[ZBCOMMETH customFontWithName:FONTPingFangSCRegular size:15],NSFontAttributeName, nil] forState:UIControlStateNormal];
        _rightBarButtonItem.action=@selector(navRightBaseButtonClick);
        _rightBarButtonItem.target=self;
        self.navigationItem.rightBarButtonItem =_rightBarButtonItem;
    }
    return _rightBarButtonItem;
}
#pragma mark-导航条左边按钮
-(void)navLeftBaseButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark-导航条右边按钮
-(void)navRightBaseButtonClick
{
}

#pragma mark- 通过颜色来生成一个纯色图片
- (UIImage*)imageWithColor:(UIColor*)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 100.0f, 99.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

#pragma mark- 寻找导航栏下的黑线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

@end
